﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace FCMGAAI
{
    public partial class FCMForm : Form
    {
        FCM1 FCM ;
        public FCMForm()
        {
            InitializeComponent();
        }

        public FCMForm(ref FCM1 _FCM)
        {
            InitializeComponent();
            FCM = _FCM;
            fillGridDataAwal();
            WriteLog("FCM Started");
            WriteLog("Jumlah Data : " + FCM.panjangBarisData.ToString());
            WriteLog("Jumlah Kriteria : " + FCM.panjangKolomParameterData.ToString());
        }

        private void fillGridDataAwal()
        {
            string [] Label = {"NamaEcoli","k1","k2","k3","k4","k5","k6","K7","Cluster"};
            string[,] Data = new string[FCM.MDataAsli.GetLength(0), Label.Length];
            for (int i = 0; i < Data.GetLength(0); i++)
            {
                Data[i, 0] = FCM.NamaLabel[i];
                Data[i, Label.Length-1] = FCM.ClusterAsli[i];
            }
            for (int i = 0; i < Data.GetLength(0); i++)
            {
                for (int j = 1; j < Label.Length-1; j++)
                {
                    Data[i, j] = FCM.MDataAsli[i, j-1].ToString();
                }
            }

            UIHelper.FillGrid(ref dataGridView1, Label, Data);
            
        }
        private void fillGridHasil()
        {
            /*
            string[] Label = { "Anggota Cluster" };
            string[,] data = new string[FCM.ClusterFCM.Length,1];
            for (int i = 0; i < data.GetLength(0); i++)
            {
                data[i,0] = FCM.letak[i].ToString();
            }
            UIHelper.FillGrid(ref dataGridView2, Label, data);
             */
            ecoliModelBindingSource.DataSource = FCM.Ecoli;
            dataGridView2.DataSource = ecoliModelBindingSource;

        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void SetComboBox(int Jumlah_Cluster)
        {
            for (int i = 1; i <= Jumlah_Cluster; i++)
            {
                comboBox1.Items.Add(i.ToString());
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int MaxIter = Convert.ToInt32(MaxIterasitextBox1.Text);
            double Akurasi = Convert.ToDouble(ErrorRatetextBox2.Text);
            int JumlahCluster = Convert.ToInt32(JumlahClustertextBox3.Text);

            WriteLog("Operasi FCM Mulai");
            Stopwatch StopWatch = new Stopwatch();
            //int MaxIter = 500;
            //double Akurasi = 0.0001;
            //int JumlahCluster = 7;

            SetComboBox(JumlahCluster);
             
            FCM.setParameter(JumlahCluster, 2, MaxIter, Akurasi);
            StopWatch.Start();
            FCM.SetMDerajatKeanggotaanAwal();
            FCM.FCMOperate();
            StopWatch.Stop();
            fillGridHasil();
            comboBox1.Enabled = true;
            WriteLog("Operasi FCM Selesai dalam waktu :" + StopWatch.Elapsed.ToString());
            //MessageBox.Show("FCM Berhasil dijalankan,..meskipun tanpa visualisasi he he");
        }

        private void ComboBoxIndexChange(object sender, EventArgs e)
        {
            if (comboBox1.Text != "")
            {
                List<EcoliModel> TempList = (from C in FCM.Ecoli where C.HasilClustering == Convert.ToInt32(comboBox1.Text) select C).ToList<EcoliModel>();
                ecoliModelBindingSource.DataSource = TempList;
                dataGridView2.DataSource = ecoliModelBindingSource;
                dataGridView2.Refresh();
            }
            else
            {
                ecoliModelBindingSource.DataSource = FCM.Ecoli;
                dataGridView2.DataSource = ecoliModelBindingSource;
            }
            
        }

        private void WriteLog(String Msg)
        {
            String Str = DateTime.Now.TimeOfDay.ToString();
            Str += "->";
            Str += Msg;
            Str += "\n";
            richTextBox1.AppendText(Str);
        }
    }
}
